package com.eralink.uhfreaderlib;

import android.serialport.SerialPort;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class SerialPortReader implements Reader {
    private static final String TAG = "SerialPortReader";

    private final String devicePath;
    private final int baud;
    private SerialPort serialPort = null;

    SerialPortReader(final String devicePath, final int baud) {
        this.devicePath = devicePath;
        this.baud = baud;
    }

    @Override
    public int open() {
        if (serialPort != null) {
            return EraLinkUHFReaderApiError.ErrAlreadyOpened;
        }

        try {
            serialPort = SerialPort.newBuilder(devicePath, baud).build();
        } catch (SecurityException e) {
            serialPort = null;
            e.printStackTrace();
            Log.e(TAG, String.format("open serial exception: %s", e.getMessage()));
            return EraLinkUHFReaderApiError.ErrOpenFailed;
        } catch (IOException e) {
            serialPort = null;
            e.printStackTrace();
            Log.e(TAG, String.format("open serial excecption: %s", e.getMessage()));
            return EraLinkUHFReaderApiError.ErrCommunicationError;
        } catch (Exception e) {
            serialPort = null;
            e.printStackTrace();
            Log.e(TAG, String.format("open serial excecption: %s", e.getMessage()));
            return EraLinkUHFReaderApiError.ErrCommunicationError;
        }

        return EraLinkUHFReaderApiError.ErrNoError;
    }

    @Override
    public int close() {
        if (null != serialPort) {
            serialPort.close();
            serialPort = null;
        }
        return 0;
    }

    public int read(final byte[] buffer, final int[] offset, final int size) {
        if (null == serialPort) {
            return EraLinkUHFReaderApiError.ErrNotOpened;
        }

        try {
            InputStream inputStream = serialPort.getInputStream();
            final int readSize = inputStream.read(buffer, offset[0], size);
            if (0 < readSize) {
                offset[0] += readSize;
            }
//            if (-1 == readSize) {
//                return EraLinkUHFReaderApiError.ErrCommunicationError;
//            }
        } catch (IOException e) {
            e.printStackTrace();
            return EraLinkUHFReaderApiError.ErrCommunicationError;
        }

        return EraLinkUHFReaderApiError.ErrNoError;
    }

    public int write(final byte[] buffer, final int offset, final int len) {
        if (null == serialPort) {
            return EraLinkUHFReaderApiError.ErrNotOpened;
        }

        try {
            OutputStream outputStream = serialPort.getOutputStream();
            outputStream.write(buffer, offset, len);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return EraLinkUHFReaderApiError.ErrCommunicationError;
        }

        return EraLinkUHFReaderApiError.ErrNoError;
    }
}
